package com.jiaai.web.zfp_back.gift;

import com.alibaba.dubbo.config.annotation.Reference;
import com.jiaai.exception.ServerException;
import com.jiaai.gift.api.IGiftApi;
import com.jiaai.gift.model.GiftVO;
import com.jiaai.gift.model.VGiftItemVO;
import com.jiaai.gift.model.VGiftType;
import com.jiaai.util.RestResultGenerator;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

/**
 * class_name:ZBGiftController
 * describe:TODO
 * create_user:张昉平
 * create_date:2017/12/19
 * create_date:14:16
 */
@RestController
@RequestMapping("/api/v1/admin/gift")
public class ZBGiftController {

    @Reference
    private IGiftApi giftApi;

    @GetMapping("/findByAll")
    @ResponseBody
    public Object allGift(@RequestParam(defaultValue = "0", required = false) Integer curr,VGiftItemVO vGiftItemVO) throws Exception {
        return RestResultGenerator.genSuccessResult(giftApi.allGift(curr,vGiftItemVO));
    }
    @PostMapping("/addGift")
    public Object addGift(@RequestBody VGiftItemVO vGiftItemVO) throws ServerException {
        try {
            giftApi.addGift(vGiftItemVO);
            return RestResultGenerator.genSuccessResult("添加成功");
        } catch (Exception e) {
            throw new ServerException("添加失败");
        }
    }



    @GetMapping("/allGiftType")
    @ResponseBody
    public Object allType(VGiftType vGiftType ) throws Exception {
        return RestResultGenerator.genSuccessResult(giftApi.allGiftType(vGiftType));
    }

    @PostMapping("/addGiftType")
    public Object addGiftType(@RequestBody VGiftType vGiftType) throws ServerException {
        try {
            giftApi.addGiftType(vGiftType);
            return RestResultGenerator.genSuccessResult("添加成功");
        } catch (Exception e) {
            throw new ServerException("添加失败");
        }
    }

    @GetMapping("/getMyAll")
    public Object getMyAll(@RequestParam(defaultValue = "0", required = false) Integer curr,GiftVO giftVO) throws ServerException{
        try {
            return RestResultGenerator.genSuccessResult(giftApi.getMyAll(curr, giftVO));
        } catch (ServerException e) {
            throw new ServerException("查询失败");
        }
    }

    /**
     * =============================================后台api==============================================================
     */
    /**
     * 获取excel文件名
     * @param vGiftItemVO
     * @return
     */
    @GetMapping("/excelname")
    public Object getExcelName(VGiftItemVO vGiftItemVO){
        try {
            System.out.println("111111111111111111111111111111111111111111");
            return RestResultGenerator.genSuccessResult(giftApi.getExcelGift(vGiftItemVO));
        } catch (Exception e) {
            throw new RuntimeException("下载失败");
        }
    }

    /**
     * 下载Excel文件
     * @param request
     * @param response
     * @param filename
     */
    @GetMapping("/excelGift")

    public void getExcel(HttpServletRequest request, HttpServletResponse response, @RequestParam String filename){
        String fullPath = "D://upload//jiaai//excel//".concat(filename);
        File downloadFile = new File(fullPath);
        ServletContext context = request.getServletContext();

        String mimeType = context.getMimeType(fullPath);
        mimeType = "application/octet-stream; charset=utf-8";

        response.setContentType(mimeType);
        response.setContentLength((int) downloadFile.length());

        String headerKey = "Content-Disposition";
        String headerValue = null;

        try {
            headerValue = "attachment; filename=".concat(new String("商品信息表.xlsx".getBytes(), "ISO-8859-1"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        response.setHeader(headerKey, headerValue);
        // Copy the stream to the response's output stream.
        try {
            InputStream myStream = new FileInputStream(fullPath);
            IOUtils.copy(myStream, response.getOutputStream());
            response.flushBuffer();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取收到礼物excel文件名
     * @param giftVO
     * @return
     */
    @GetMapping("/getName")
    public Object getName(GiftVO giftVO){
        try {
            System.out.println("111111111111111111111111111111111111111111");
            return RestResultGenerator.genSuccessResult(giftApi.getGift(giftVO));
        } catch (Exception e) {
            throw new RuntimeException("下载失败");
        }
    }

    /**
     * 下载获取收到Excel文件
     * @param request
     * @param response
     * @param filename
     */
    @GetMapping("/getGift")

    public void getGift(HttpServletRequest request, HttpServletResponse response, @RequestParam String filename){
        String fullPath = "D://upload//jiaai//excel//".concat(filename);
        File downloadFile = new File(fullPath);
        ServletContext context = request.getServletContext();

        String mimeType = context.getMimeType(fullPath);
        mimeType = "application/octet-stream; charset=utf-8";

        response.setContentType(mimeType);
        response.setContentLength((int) downloadFile.length());

        String headerKey = "Content-Disposition";
        String headerValue = null;

        try {
            headerValue = "attachment; filename=".concat(new String("收到礼物列表.xlsx".getBytes(), "ISO-8859-1"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        response.setHeader(headerKey, headerValue);
        // Copy the stream to the response's output stream.
        try {
            InputStream myStream = new FileInputStream(fullPath);
            org.apache.commons.io.IOUtils.copy(myStream, response.getOutputStream());
            response.flushBuffer();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /**
     * 获取送出礼物excel文件名
     * @param giftVO
     * @return
     */
    @GetMapping("/toName")
    public Object toName(GiftVO giftVO){
        try {
            System.out.println("111111111111111111111111111111111111111111");
            return RestResultGenerator.genSuccessResult(giftApi.toGift(giftVO));
        } catch (Exception e) {
            throw new RuntimeException("下载失败");
        }
    }

    /**
     * 下载获取收到Excel文件
     * @param request
     * @param response
     * @param filename
     */
    @GetMapping("/toGift")

    public void toGift(HttpServletRequest request, HttpServletResponse response, @RequestParam String filename){
        String fullPath = "D://upload//jiaai//excel//".concat(filename);
        File downloadFile = new File(fullPath);
        ServletContext context = request.getServletContext();

        String mimeType = context.getMimeType(fullPath);
        mimeType = "application/octet-stream; charset=utf-8";

        response.setContentType(mimeType);
        response.setContentLength((int) downloadFile.length());

        String headerKey = "Content-Disposition";
        String headerValue = null;

        try {
            headerValue = "attachment; filename=".concat(new String("送出礼物列表.xlsx".getBytes(), "ISO-8859-1"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        response.setHeader(headerKey, headerValue);
        // Copy the stream to the response's output stream.
        try {
            InputStream myStream = new FileInputStream(fullPath);
            org.apache.commons.io.IOUtils.copy(myStream, response.getOutputStream());
            response.flushBuffer();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }




}
